package com.example.firstdemo.ui.screens.components

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.statusBars
import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ProvideTextStyle
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import com.example.firstdemo.ui.theme.AppTheme
import com.example.firstdemo.ui.theme.FontSize
import com.example.firstdemo.ui.theme.eColors

@Composable
fun TopAppBar(
    navigationIcon: (@Composable () -> Unit)? = null,
    actions: (@Composable () -> Unit)? = null,
    content: (@Composable () -> Unit)
) {
    Surface(
        color = MaterialTheme.eColors.fill.darker,
        contentColor = MaterialTheme.eColors.text.regular,
        modifier = Modifier
            .background(MaterialTheme.eColors.fill.darker)
            .windowInsetsPadding(WindowInsets.statusBars)
    ) {
        Row(
            Modifier
                .fillMaxWidth()
                .wrapContentHeight()
                .height(IntrinsicSize.Min)
        ) {
            if (navigationIcon != null) {
                navigationIcon()
            }
            Box(
                contentAlignment = Alignment.Center,
                modifier = Modifier
                    .weight(1f)
                    .fillMaxHeight()
            ) {
                ProvideTextStyle(
                    value = LocalTextStyle.current.copy(
                        color = MaterialTheme.eColors.text.regular,
                        fontSize = FontSize.xl, // 设置默认字体大小
                        fontWeight = FontWeight.Bold // 可选：其他默认样式
                    )
                ) {
                    content()
                }
            }
            if (actions != null) {
                actions()
            }
        }
    }
}

@Preview()
@Composable
fun TopAppBarPreview() {
    AppTheme() {
        TopAppBar(
            navigationIcon = {
                IconButton(onClick = { }) {
                    Icon(
                        imageVector = Icons.AutoMirrored.Filled.ArrowBack,
                        contentDescription = "Localized description"
                    )
                }
            },
            actions = {
                IconButton(onClick = { /* do something */ }) {
                    Icon(
                        imageVector = Icons.Filled.Menu,
                        contentDescription = "Localized description"
                    )
                }
            },
        ) {
            Text(
                "故障详情",
                maxLines = 1,
                overflow = TextOverflow.Ellipsis,
            )
        }
    }
}